package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.pojo.Dish;
import com.itheima.reggie.pojo.DishFlavor;
import com.itheima.reggie.pojo.Setmeal;
import com.itheima.reggie.pojo.SetmealDish;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;

import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private DishService dishService;

    /**
     * 保存套餐,以及菜品
     */
    @CacheEvict(cacheNames = "setmeal:categoryid",allEntries = true)
    @PostMapping
    public R save(@RequestBody SetmealDto setmealDto){
        setmealService.saveWithDish(setmealDto);
        return R.success("保存成功");
    }

    /**
     * 套餐分页展示
     */
    @GetMapping("page")
    public R page(Integer page,Integer pageSize,String name){
        //创建套餐分页器
        Page<Setmeal> pageInfo1 = new Page<>(page,pageSize);
        //分页条件
        LambdaQueryWrapper<Setmeal> qw = Wrappers.lambdaQuery(Setmeal.class).
                like(StringUtils.isNotBlank(name), Setmeal::getName, name);
        setmealService.page(pageInfo1,qw);

        //查询套餐dto分页器
        Page<SetmealDto> pageInfo2 = new Page<>(page,pageSize);
        //拷贝基本数据
        BeanUtils.copyProperties(pageInfo1,pageInfo2,"records");

        //创建SetmealDto对象集合
        ArrayList<SetmealDto> setmealDtos = new ArrayList<>();
        //遍历setmeal对象集合,SetmealDto对象=setmeal对象+categoryName(分类名)
        for (Setmeal setmeal : pageInfo1.getRecords()) {
            SetmealDto setmealDto = new SetmealDto();
            //对象数据拷贝
            BeanUtils.copyProperties(setmeal,setmealDto);
            Long categoryId = setmeal.getCategoryId();
            //setmealDto的分类名赋值
            setmealDto.setCategoryName(categoryService.getById(categoryId).getName());
            //对象封装进集合
            setmealDtos.add(setmealDto);
        }
        //集合装入分页数据中
        pageInfo2.setRecords(setmealDtos);
        return R.success(pageInfo2);
    }

    /**
     * 通过ids删除套餐
     */
    @CacheEvict(cacheNames = "setmeal:categoryid",allEntries = true)
    @DeleteMapping
    public R deleteByIds(Long[] ids){
        int count = setmealService.deleteByIds(ids);
        if (count == 0){
            return R.error("商品售卖中,无法删除");
        }
        return R.success("成功删除"+count+"条套餐");
    }


    /**
     * 通过ids更改停售起售状态
     */
    @CacheEvict(cacheNames = "setmeal:categoryid",allEntries = true)
    @PostMapping("status/{status}")
    public R updateStatus(@PathVariable Integer status,Long[] ids){
        LambdaUpdateWrapper<Setmeal> updateWrapper = Wrappers.lambdaUpdate(Setmeal.class)
                .set(Setmeal::getStatus, status)
                .in(Setmeal::getId, ids);

        setmealService.update(updateWrapper);

        return R.success("更新成功");
    }

    /**
     * 修改
     */
    //1.回显数据
    @GetMapping("{id}")
    public R getById(@PathVariable Long id){
        SetmealDto setmealDto = setmealService.updateAll(id);
        return R.success(setmealDto);
    }

    //2.修改数据
    @CacheEvict(cacheNames = "setmeal:categoryid",allEntries = true)
    @PutMapping
    public R updateById(@RequestBody SetmealDto setmealDto){
        setmealService.updateWithDish(setmealDto);
        return R.success("更新成功");
    }


    /**
     * 通过分类id查询套餐
     */
    @Cacheable(value = "setmeal:categoryid", key = "#categoryId", unless = "#result.data.size() == 0")
    @GetMapping("list")
    public R<List<Setmeal>> list(Long categoryId,Integer status){
        LambdaQueryWrapper<Setmeal> eq = Wrappers.lambdaQuery(Setmeal.class)
                .eq(Setmeal::getCategoryId, categoryId)
                .eq(Setmeal::getStatus, status);
        List<Setmeal> list = setmealService.list(eq);

        return R.success(list);
    }

    /**
     * 根据套餐id,查看套餐菜品详情
     */
    @GetMapping("dish/{setmealId}")
    public R setmealDishList(@PathVariable Long setmealId){
        List<DishDto> dishDtos = new ArrayList<>();
        LambdaQueryWrapper<SetmealDish> wrapper = Wrappers.lambdaQuery(SetmealDish.class)
                .eq(SetmealDish::getSetmealId, setmealId);
        List<SetmealDish> setmealDishlist = setmealDishService.list(wrapper);

        //遍历SetmealDishlist,取出菜品id集合

        for (SetmealDish setmealDish : setmealDishlist) {
            DishDto dishDto = new DishDto();
            //得到dish份数数据,拷贝份数数据
            Integer copies = setmealDish.getCopies();
            dishDto.setCopies(copies);
            //根据菜品Id查询菜品
            Dish dish = dishService.getById(setmealDish.getDishId());
            //拷贝dish数据
            BeanUtils.copyProperties(dish,dishDto);
            dishDtos.add(dishDto);
        }
        return R.success(dishDtos);
    }
}
